package com.nny.alarm.eidt;

import android.os.Handler;
import android.os.Message;

import com.nny.alarm.audio.RingPlayer;
import com.nny.alarm.view.DialTimeView;
import com.nny.alarm.view.DialViewAdapter;
import com.uxwine.alarm.mon.RingManager;

public class RingEditor implements IAlarmEditor
{
	AlarmEditor mEditor;
	DialViewAdapter mAdapter;
	RingPlayer mRing;
	MHandler mHandler;
	
	private static final int MSG_PLAYRING = 1;
	
	public RingEditor(AlarmEditor a)
	{
		mEditor = a;
		mRing = new RingPlayer(a.mCtx);
		mHandler = new MHandler();
	}

	@Override
	public void edit()
	{
		final DialTimeView dtv = mEditor.mDial;
		mAdapter = new RingDVAdpater(mEditor.mCtx);
		dtv.setAdpater(mAdapter);
		mAdapter.notifyDataChanged();
	}

	@Override
	public boolean next()
	{
		stop();
		mEditor.step(AlarmEditor.STEP_CYCLE);
		return true;
	}

	@Override
	public void onMarked(int index)
	{
		mEditor.mAlarm.alert = RingManager.getRingUri(index);
	}

	@Override
	public void onMarkChange(int index)
	{
		//TODO mAlarm.setSound(index);
		
		stop();
		mHandler.sendEmptyMessageDelayed(MSG_PLAYRING, 1000);
	}
	
	public void stop()
	{
		mHandler.removeMessages(MSG_PLAYRING);
		mRing.stop();
	}

	private class MHandler extends Handler
	{

		@Override
		public void handleMessage(Message msg) {
			if(msg.what == MSG_PLAYRING)
			{
				mRing.play((int)mAdapter.getValue(mAdapter.getPosition()));
			}
		}
	}

	@Override
	public void over()
	{
		stop();
	}
	
	
	
}
